package leetcode.editor.cn;

import utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树的层序遍历
 * @author qulq
 * @date 2025-03-04 21:58:42
 */
public class T102_BinaryTreeLevelOrderTraversal{
	 public static void main(String[] args) {
	 	 // 测试代码
	 	 Solution solution = new T102_BinaryTreeLevelOrderTraversal().new Solution();
	 }
	 
// 力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    public List<List<Integer>> levelOrder(TreeNode root) {
		List<List<Integer>> ans = new ArrayList<>();
		recursion(root, 1, ans);
		return ans;

    }

	private void recursion(TreeNode root, int depth, List<List<Integer>> ans) {
		if (root == null) {
			return;
		}
		if (ans.size() < depth) {
			ans.add(new ArrayList<>());
		}
		ans.get(depth - 1).add(root.val);
		recursion(root.left, depth + 1, ans);
		recursion(root.right, depth + 1, ans);
	}

}
//leetcode submit region end(Prohibit modification and deletion)

}